﻿<?php 
/** 
 * 玩自拍模块
 **/
class Module_autodyne{
	static private $white_uins = array(1 , 2 , 3 , 9);
	/**
	 * 构造函数
	 */
	function __construct(){
	
	}
	/**
	 * 玩自拍首页
	 * 
	 */
	function index(){
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		$uin = SessionInfo('uin');
		
		$smarty = loadSmarty();
		
		// 获取用户数据
		loadLib('Autodyne');
		//获取数据总量
		$autodyne_count = Autodyne::getCount();
		
		//设置每行10条数据
		$per_page = 5;
		if($page <= 0){
			$page = 1;
		}
		
		//计算总页数
		$autodyne_list = Autodyne::getList($page , $per_page);
		$send_uin_list = array();
		foreach($autodyne_list as $row) {
			$send_uin_list[] = $row['uin'];
			$path_list[] = $row['time'];
		}
		$is_white_uin = $this->_is_white_uin($uin);
		if(!empty($send_uin_list)) {
			loadLib('User');
			
			$send_uin_info = User::getBaseInfo($send_uin_list);
			//拼接图片路径
			foreach ($path_list as $k => $f){
				$path[] = Autodyne::getDate($send_uin_list[$k] , $path_list[$k]);
			}
			
			
			foreach($autodyne_list as $k => $row) {
				$send_uin = $row['uin'];
				$autodyne_list[$k]['send_uin_name'] = isset($send_uin_info[$send_uin]) ? $send_uin_info[$send_uin]['name'] : '';
				$autodyne_list[$k]['send_uin_sex'] = isset($send_uin_info[$send_uin]) ? $send_uin_info[$send_uin]['sex'] : '';
				$autodyne_list[$k]['can_reply'] = $is_white_uin == $uin ? true : false;
				$autodyne_list[$k]['photo'] = isset($path[$k]) ? $path[$k] : '';
				//计算已回复人个数
				$autodyne_list[$k]['count'] = count($autodyne_list[$k]['reply']);
			}
		}
	//	print_r($autodyne_list);
		$smarty->assign('autodyne_count' , $autodyne_count);
		$smarty->assign('autodyne_list' , $autodyne_list);
		$smarty->assign('per_page' , $per_page);
		
		return $smarty->fetch('autodyne/list.tpl.php');
	}
	/**
	 * 是否是白名单用户
	 */
	private function _is_white_uin($uin) {
		return in_array($uin , self::$white_uins);
	}
	/**
	 * 自拍上传页面
	 */
	function openplay(){
		$smarty = loadSmarty();
		
		return $smarty->fetch('autodyne/index.tpl.php');
	}
	//上传图片,发布信息页面
	function photos() {
		$uptypes=array('image/jpg',  //上传文件类型列表
		'image/jpeg',
		'image/png',
		'image/pjpeg',
		'image/gif',
		'image/bmp',
		'application/x-shockwave-flash',
		'image/x-png');
		
		$max_file_size = 5000000;   //上传文件大小限制, 单位BYTE
		$upimg_width = 240;
		$upimg_height = 320;
		
		$content = isset($_POST['content']) ? trim($_POST['content']) : null;
		//print_r($content);
		// 检查是否有内容
		if($content === null || strlen($content) == 0) {
			return View::error('内容不能为空');
		}
		
		// 长度校验
		if(strlen($content) > 255) {
			return View::error('内容超过了长度限制，最长是140个字');
		}
		
		if ($_SERVER['REQUEST_METHOD'] == 'POST')
		{

			//是否存在文件
			if(!empty($_FILES)){
				if (!is_uploaded_file($_FILES["upfile"]["tmp_name"])){
					return View::error('没有选择要上传的照片或文件不存在！');
				}
			}
			
			$file = isset($_FILES["upfile"]) ? $_FILES["upfile"] : null;
		
			if($max_file_size < $file["size"])
			//检查文件大小
			{
				return View::error('文件太大！');
			 }
			
			if(!in_array($file["type"], $uptypes))
			//检查文件类型
			{
				return View::error('只能上传图像文件或Flash！');
			}
			
			
			$upload = date('Y/md');//按月份创建一个子文件夹
			$destination_folder = "autodyne/$upload/"; //上传文件路径
			
			$uin = SessionInfo('uin');		
			
			$filename = $file["tmp_name"];
			$image_size = getimagesize($filename);
			$pinfo = pathinfo($file["name"]);
			//$ftype = $pinfo["extension"];
			$time = time();
			$destination = $uin ."_". $time .".jpg"; //图片名字
			$path = $destination_folder.$destination;  
			//dprint_r($path);
			// 生成图片
			loadLib('Thumb');
			$img_info = Thumb::genarate($filename , $destination_folder , $destination , $upimg_width , $upimg_height);
				
			if(empty($img_info) || !file_exists($path)) {
				return View::error('图片上传失败');
			}
			//发布消息
			loadLib('Autodyne');
			$code = Autodyne::add($uin , $content , $time);
			if($code === 0) {
				loadLib('Operate');
				Operate::getAdd($uin , 0 , 'autodyne');		
				header('Location: ' . SITEURL . '/?m=autodyne&sid=' . sessionInfo('skey') );
			} else {
				return View::error('系统繁忙，请稍后再试' , $code);
			}
		}
	}
	/**
	 * 跳转回复页面
	 */
	function setsend(){
		$people_uin = isset($_POST['uin']) ? intval($_POST['uin']) : null;
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		header('Location: ' . SITEURL . '/?m=autodyne&a=send&uin=' . $people_uin . '&id=' . $id . '&sid=' . sessionInfo('skey') );
	}
	/**
	*玩自拍回复界面
	*/
	function send(){
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		$people_uin = isset($_GET['uin']) ? intval($_GET['uin']) : null;
		$id = isset($_GET['id']) ? intval($_GET['id']) : null;
		//print_r($people_uin);
		$uin = SessionInfo('uin');
		
		$smarty = loadSmarty();
		
		// 获取投诉数据
		loadLib('Autodyne');
		//获取数据
		$autodyne_send = Autodyne::getSend($id , $people_uin);
		
		if(empty($autodyne_send['id'])){
			return View::error('该信息不存在或已被删除' );
		}
		$path = Autodyne::getDate($people_uin , $autodyne_send['time']);
			$send_uin_list = array();
			$send_uin_list[] = $autodyne_send['uin'];
				
				if(!empty($autodyne_send['reply'])) {
					foreach($autodyne_send['reply'] as $reply_row) {
						$send_uin_list[] = $reply_row['uin'];
					}
				}
			
			if(!empty($send_uin_list)) {
				loadLib('User');
				$send_uin_info = User::getBaseInfo($send_uin_list);
				
				$send_uin = $autodyne_send['uin'];
				$autodyne_send['photo'] = isset($path) ? $path : '';
				$autodyne_send['send_uin_name'] = isset($send_uin_info[$send_uin]) ? $send_uin_info[$send_uin]['name'] : '';
				$autodyne_send['send_uin_sex'] = isset($send_uin_info[$send_uin]) ? $send_uin_info[$send_uin]['sex'] : '';
				
				if(!empty($autodyne_send['reply'])) {
					foreach($autodyne_send['reply'] as $j => $reply_row) {
						$reply_uin = $reply_row['uin'];	
						$autodyne_send['reply'][$j]['name'] = isset($send_uin_info[$reply_uin]) ? $send_uin_info[$reply_uin]['name'] : '';
						$autodyne_send['reply'][$j]['sex'] = isset($send_uin_info[$reply_uin]) ? $send_uin_info[$reply_uin]['sex'] : '';
						$autodyne_send['reply'][$j]['id'] = $j + 1;
					}
				}
			}
			
		//设置每行10条数据
			$per_page = 10;
			if($page <= 0){
				$page = 1;
			}
			$count = count($autodyne_send['reply']);
		//	print_r($autodyne_send['reply']);
			$page = ($page-1) * $per_page;
			//print_r($page);
			$send = array_slice($autodyne_send['reply'],$page,$per_page);
		//print_r($send);
			$smarty->assign('autodyne_send' , $autodyne_send);
			$smarty->assign('send' , $send);
			$smarty->assign('count' , $count);
			$smarty->assign('id' , $id);
			$smarty->assign('people_uin' , $people_uin);
			$smarty->assign('per_page' , $per_page);
			return $smarty->fetch('autodyne/send.tpl.php');	
	}
	/**
	*玩自拍回复操作
	*/
	function upSend(){
		$people_uin = isset($_POST['uin']) ? intval($_POST['uin']) : null;
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		$content = isset($_POST['content']) ? trim($_POST['content']) : null;
		
		if(empty($id)) {
			return View::error('参数错误' );
		}
		
		// 检查是否有内容
		if($content === null || strlen($content) == 0) {
			
			return View::error('内容不能为空');
		}
		
		// 长度校验
		if(strlen($content) > 255) {
			return View::error('内容超过了长度限制，最长是140个字');
		}

		$uin = SessionInfo('uin');
		
		loadLib('Autodyne');
		//print_r($people_uin);
		$org_autodyne = Autodyne::getSend($id , $people_uin);
		if(empty($org_autodyne)) {
			return View::error('该信息不存在或已被删除');
		}

		//print_r($org_amorism);
		$code = Autodyne::getUpSend($uin , $people_uin , $id , $content , $org_autodyne);
		
		if($code === 0) {
			
			if($uin != $people_uin) {
				loadLib('Operate');
				Operate::getAdd($uin , $people_uin , 'autodyne_reply');	
				$sysmsg_to_uin = $org_autodyne['uin'];
				loadLib('SysMsg');
				SysMsg::sendSystem($sysmsg_to_uin , array(
					'send_uin' => $uin,
					'id' => $id,
				) , 1008);
			}
			//return View::success('回复成功！');		
			header('Location: ' . SITEURL . '/?m=autodyne&a=send&uin=' . $people_uin . '&id=' . $id . '&sid=' . sessionInfo('skey') );
		} else {
			return View::error('该信息已删除，操作失败' , $code);
		}
	}
	/**
	*删除玩自拍内容
	*/
	function delete(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		$people_uin = isset($_POST['uin']) ? intval($_POST['uin']) : null;
		$photo = isset($_POST['photo']) ? trim($_POST['photo']) : null;
		//print_r($photo);
		
		if(empty($id)) {
			return View::error('参数错误' );
		}
		$uin = SessionInfo('uin');
		
		if( !$this->_is_white_uin($uin) ) {
			return View::error('您不是管理员，没有删除权限');
		}
		
		loadLib('Autodyne');

		$code = Autodyne::delete($id , $people_uin);
			
		if($code === 0) {	
			unlink($photo);	//删除图片		
			header('Location: ' . SITEURL . '/?m=autodyne&sid=' . sessionInfo('skey') );
		} else {
			return View::error('系统繁忙' , $code);
		}
		
	}
}

# end of script